package com.longtop.framework.sys.role.web;

import java.util.HashMap;
import java.util.Map;

import com.longtop.framework.base.BaseAction;
import com.longtop.framework.security.BaseCheckedException;
import com.longtop.framework.security.SessionObject;
import com.longtop.framework.sys.role.model.TSysUserRole;
import com.longtop.framework.sys.role.service.RoleService;
import com.longtop.framework.util.WebUtil;
import com.longtop.intelliweb.action.IActionContext;
import com.longtop.intelliweb.action.IInitContext;
import com.longtop.intelliweb.view.Dataset;

/**
 * 
 * @author 孙景弘
 */
public class RoleUserAction extends BaseAction {

	private RoleService service;
	private final String DATASET_ROLE_USER = "Dataset_RoleUser";
	// 查询条件：
	private Map<String, Object> queryMap = new HashMap<String, Object>();

	public RoleUserAction() {
		service = this.getBean(RoleService.BEAN_ID);
	}

	/**
	 * 系统共用：初始化调用
	 */
	public void init(IInitContext context) {
		super.init(context);
	}

	/**
	 * 本页jsp：初始化业务数据(覆写父类方法)
	 */
	public void thisPageInit(IInitContext context) {
		// 查询roleId 已有的用户信息
		Dataset dataset = context.getDataset(DATASET_ROLE_USER);
		if (!WebUtil.validateDatasetExist(this, dataset, DATASET_ROLE_USER)) {
			return;
		}
		String roleId = this.getParameter(context, "roleId");
		queryMap.put("roleId", roleId);
		service.queryForPageByIbatis("sysRoleSql.selectUserRoleList", null, queryMap, dataset, false);
	}

	/**
	 * 保存角色分配多用户
	 * 
	 * @param context
	 */
	public void save(IActionContext context) {
		Dataset dataset = context.getDataset(DATASET_ROLE_USER);
		if (!WebUtil.validateDatasetChange(this, dataset, DATASET_ROLE_USER)) {
			return;
		}
		SessionObject sessionObject = this.getSessionObject(context);

		try {
			Map objectList = WebUtil.datasetToObjectList(dataset, "id", TSysUserRole.class, "userName,orgName");
			Integer[] count = service.saveUserRole(objectList, sessionObject);
			String msg = WebUtil.getSubmitSuccessInfo(count[0], count[1], count[2]);
			this.hintInfoMessage(msg);
		} catch (BaseCheckedException ex) {
			this.hintErrorMessage(ex.getMessage());
		} catch (Exception e) {
			e.printStackTrace();
			this.hintErrorMessage(e.getMessage());
		}
	}

	/**
	 * 翻页查询
	 * 
	 * @param context
	 */
	public void load(IActionContext context) {
		commonQuery(context, false);
	}

	/**
	 * 查询
	 * 
	 * @param context
	 */
	public void search(IActionContext context) {
		commonQuery(context, true);
	}

	/**
	 * 通用查询
	 * 
	 * @param context
	 * @param customAction
	 */
	private void commonQuery(IActionContext context, boolean isFirstPage) {
		Dataset dataset = context.getDataset(DATASET_ROLE_USER);
		// 对于自定义查询方法，必须指定从第一页开始查询
		if (isFirstPage) {
			dataset.setPageNo(1);
		}
		String roleId = this.getParameter(context, "roleId");
		queryMap.put("roleId", roleId);

		service.queryForPageByIbatis("sysRoleSql.selectUserRoleList", null, queryMap, dataset, false);
	}
}
